﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.ServiceHelper.Excel;
using Kingdee.BOS.App.Data;



//AbstractBillPlugIn
namespace B2CProject
{
    public class ImportEntryEdit : AbstractBillPlugIn
    {
        /// <summary>
        /// 菜单点击事件
        /// </summary>
        /// <param name="e"></param>
        /// 
        public string FInspectext;
        public string FInspecData;
        public string FBillNo;

        public override void EntryBarItemClick(BarItemClickEventArgs e)
        {

            if (e.BarItemKey == "tb_JD_ImportEntry")
            {
                // 显示文件上传界面，上传Excel文件
                DynamicFormShowParameter showParam = new DynamicFormShowParameter();
                //kf77eb3f3c38f4dcc8b47e6b6da717a05 本地测试
               
                showParam.FormId = "kf77eb3f3c38f4dcc8b47e6b6da717a05";
                this.View.ShowForm(showParam,
                    new Action<FormResult>((formResult) =>
                    {
                        if (formResult != null && formResult.ReturnData != null)
                        {
                            string fullFileName = formResult.ReturnData.ToString();
                            this.DoImportEntry(fullFileName);
                        }
                    }));
            }
        }

       public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
        {
            string strsql, FCount, FChecker, FID;
            bool FBoolBCC,FBollData;

            if (e.BarItemKey.EqualsIgnoreCase("tb_JD_SETB2C"))
            {
                //生成B2C订单
                string FBillNo = this.Model.GetValue("FBillNo").ToString();

                strsql = "select count(*) from KD_BCC_IMPORT where FBillNo ='" + FBillNo + "'";
                FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                if (FCount =="0")
                {
                    throw new KDBusinessException("","单据还未保存，功能不可用");
                    //this.View.ShowMessage("Hello world!", MessageBoxType.Notice);
                }
                else
                {
                    strsql = "select isnull(F_PAEZ_Checker,0) from KD_BCC_IMPORT where FBillNo ='" + FBillNo + "'";
                    FChecker = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                    if (FChecker == "0")
                    {
                        throw new KDBusinessException("", "单据还未审核，功能不可用");
                       // this.View.ShowMessage("Hello world!", MessageBoxType.Notice);
                    }
                    else
                    {

                        strsql = "select FID from KD_BCC_IMPORT where FBillNo ='" + FBillNo + "'";
                        FID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);


                        strsql = "select count(*) from KD_BCC_IMPORTentry where FID=" + FID + " and F_BCB_BILLNO<>''";
                        FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                        if (FCount != "0")
                        {
                            FJCBCCNo(FID);
                        }



                        strsql = "select count(*) from KD_BCC_IMPORTentry where FID=" + FID + " and F_BCB_BILLNO=''";
                        FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                        if (FCount == "0")
                        {
                            throw new KDBusinessException("", "B2C订单已生成，不能重复生成！");
                        }
                        else
                        {

                            FBoolBCC = FInspectBCC(FID);

                            if (FBoolBCC == true)
                            {
                                FBollData = FInspectDate(FID);

                                if (FBollData == true)
                                {

                                    FSETBCC(FID);
                                    this.View.UpdateView("F_PAEZ_Entity");
                                    this.View.ShowMessage("B2C订单生成成功，请检查", MessageBoxType.Notice);
                                }
                                else
                                {
                                    throw new KDBusinessException("", FInspecData);
                                }

                            }
                            else
                            {
                                throw new KDBusinessException("", FInspectext);
                            }

                        }
                    }
                }

            }

        }


       /// <summary>
       /// 开始导入单据体
       /// </summary>
       /// <param name="fullFileName">Excel文件</param>
       /// <remarks>
       /// 特别说明：此函数仅演示如何把Excel转换为DataSet，
       /// 并读取DataSet内容，填写到单据体，
       /// 未进行任何的安全处理，实际生产环境，需要对数据的合法性进行严格的判断
       /// </remarks>
       /// 
       private void DoImportEntry(string fullFileName)
       {

           int i;
           using (ExcelOperation helper = new ExcelOperation(this.View))
           // 利用ExcelOperation对象，把xml文件，转为DataSet对象
           // 参数说明：
           // filePath : 完整的文件名，包含了物理目录
           // dataStartIndex : 数据开始行索引，从0开始。通常第一行为标题，第二行开始为数据行
           // colNameIndex : 列名所在行索引，从0开始。如此参数为0，表明第一行为列名行
           {
               DataSet ds = helper.ReadFromFile(fullFileName, 1, 0);

               DataTable dt = ds.Tables[0];


               for (i = this.Model.GetEntryRowCount("F_PAEZ_Entity"); i >= 0; i--)
               {

                   this.Model.DeleteEntryRow("F_PAEZ_Entity", i);

               }
               int rowIndex = this.Model.GetEntryRowCount("F_PAEZ_Entity");
               foreach (DataRow dataRow in dt.Rows)
               {
                   this.Model.CreateNewEntryRow("F_PAEZ_Entity");
                   this.Model.SetEntryCurrentRowIndex("F_PAEZ_Entity", rowIndex);
                   this.Model.SetItemValueByNumber("FNETORDERSOURCE", Convert.ToString(dataRow["来源网店"]), rowIndex);
                   this.Model.SetValue("F_PAEZ_LYBILLNO", dataRow["订单号"], rowIndex);
                   this.Model.SetValue("F_SP_NUMBER", dataRow["商品编码"], rowIndex);
                   this.Model.SetValue("F_SP_NAME", dataRow["商品名称"], rowIndex);
                   this.Model.SetValue("F_PAEZ_DZ", dataRow["详细地址"], rowIndex);
                   this.Model.SetValue("F_PAEZ_PO", dataRow["联系人"], rowIndex);
                   this.Model.SetValue("F_PAEZ_LXFS", dataRow["联系方式"], rowIndex);
                   this.Model.SetValue("F_CG_PRICE", dataRow["采购价格"], rowIndex);
                   this.Model.SetValue("F_CG_QTY", dataRow["采购数量"], rowIndex);
                   this.Model.SetValue("F_CG_AMOUNT", dataRow["采购金额"], rowIndex);
                   double FDODate = double.Parse(dataRow["订购时间"].ToString());
                   DateTime FDate = DateTime.FromOADate(FDODate);
                   this.Model.SetValue("F_XS_DATES", FDate, rowIndex);
                   this.Model.SetItemValueByNumber("F_PAEZ_ORGID", Convert.ToString(dataRow["库存组织"]), rowIndex);
                   this.Model.SetItemValueByNumber("F_PAEZ_STOCK", Convert.ToString(dataRow["仓库"]), rowIndex);
                   // TODO : 设置其他字段值，此处略
                   rowIndex++;
               }
           }

       }

       public bool FInspectDate(string FID)
       {
           string strsql, FCount, F_PAEZ_LYBILLNO;
           string F_SP_Number, F_PAEZ_DZ, F_PAEZ_PO, F_PAEZ_LXFS, FNETORDERSOURCE;
           string FSALEORGID, F_PAEZ_PRODUCTID, F_PAEZ_TJID, FPHONE;
           string FREGION, FCITY, FCOUNTY;
           string [] s;
           bool FJG = true;
           int i, l, t;
           string FADDRESSID = "";

           string FREGIONID = "";
           string FCITYID = "";
           string FCOUNTYID = "";
           string FADDRESS = "";
           string FFULLADDRESS = "";

           string FREGIONNo = "";
           string FCITYNo = "";
           string FCOUNTYNo = "";


           string F_SP_NumberAll ; 

           FInspecData = "";
           DataSet DSA;
           DataTable DTA;
           DataSet DSB;
           DataTable DTB;
           DataSet DSC;
           DataTable DTC;
           DataSet DS;
           DataTable DT;


           //string FCOUNTRY = "中国";

           strsql = "/*dialect*/ select distinct F_PAEZ_LYBILLNO from KD_BCC_IMPORTEntry where FID =" + FID + " and  F_BCB_BILLNO=''";
           DSA = DBUtils.ExecuteDataSet(this.Context, strsql);
           DTA = DSA.Tables[0];
           if (DTA.Rows.Count > 0)
           {
               F_SP_NumberAll = "";
               for (i = 0; i < DTA.Rows.Count; i++)
               {
                    
                   F_PAEZ_LYBILLNO = DTA.Rows[i]["F_PAEZ_LYBILLNO"].ToString();
                   //检测商品SKU
                   strsql = "/*dialect*/ select FNETORDERSOURCE, F_SP_Number,F_PAEZ_DZ,F_PAEZ_PO,F_PAEZ_LXFS  from KD_BCC_IMPORTEntry where FID =" + FID + " and F_PAEZ_LYBILLNO='" + F_PAEZ_LYBILLNO + "'";
                   DSB = DBUtils.ExecuteDataSet(this.Context, strsql);
                   DTB = DSB.Tables[0];
                   if (DTB.Rows.Count > 0)
                   {
                       for (l = 0; l < DTB.Rows.Count; l++)
                       {
                           FNETORDERSOURCE = DTB.Rows[l]["FNETORDERSOURCE"].ToString();
                           F_SP_Number = DTB.Rows[l]["F_SP_Number"].ToString();

                           //网店对应销售组织
                           strsql = "/*dialect*/ select FSALEORGID from T_ECC_SHOP where fid =" + FNETORDERSOURCE + "";
                           FSALEORGID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);


                           ///检测SKU  测试PAEZ_t_Cust_Entry100035  PAEZ_t_Cust_Entry100036 正式 PAEZ_t_Cust_Entry100012  PAEZ_t_Cust_Entry100013
                           strsql = "/*dialect*/ SELECT count(*) FROM PAEZ_t_Cust_Entry100013 WHERE F_PAEZ_SKUID =" + F_SP_Number + " and FID in ( " +
                                    "select FID from PAEZ_t_Cust_Entry100012 where FUSEORGID = " + FSALEORGID + ")";
                           FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                           if (FCount != "0")
                           {
                               strsql = "/*dialect*/ SELECT F_PAEZ_PRODUCTID FROM PAEZ_t_Cust_Entry100013 WHERE F_PAEZ_SKUID =" + F_SP_Number + " and FID in ( " +
                                        "select FID from PAEZ_t_Cust_Entry100012 where FUSEORGID = " + FSALEORGID + ")";
                               F_PAEZ_PRODUCTID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                               strsql = "/*dialect*/ SELECT F_PAEZ_TJID FROM PAEZ_t_Cust_Entry100013 WHERE F_PAEZ_SKUID =" + F_SP_Number + " and FID in ( " +
                               "select FID from PAEZ_t_Cust_Entry100012 where FUSEORGID = " + FSALEORGID + ")";
                               F_PAEZ_TJID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                               if (F_PAEZ_PRODUCTID == "0" && F_PAEZ_TJID == "0" && F_SP_NumberAll.IndexOf(F_SP_Number) == -1)
                               {
                                   FInspecData = FInspecData + "来源订单号:" + F_PAEZ_LYBILLNO + ",SKU码" + F_SP_Number + "未找到对应的物料或套件！";
                                   F_SP_NumberAll = F_SP_NumberAll + "," + F_SP_Number;
                                   FJG = false;
                               }
                               else
                               {
                                   strsql = "update KD_BCC_IMPORTentry  set F_PAEZ_WL =" + F_PAEZ_PRODUCTID + " ,F_PAEZ_TJ= " + F_PAEZ_TJID + " where  FNETORDERSOURCE =" + FNETORDERSOURCE + " and   F_SP_Number = " + F_SP_Number + " and FID = " + FID + " ";
                                   DBUtils.Execute(this.Context, strsql);
                               }
                           }
                           else
                           {
                               FInspecData = FInspecData + "来源订单号:" + F_PAEZ_LYBILLNO  +",SKU码" + F_SP_Number + "未找到对应的物料或套件！";
                               F_SP_NumberAll = F_SP_NumberAll + "," + F_SP_Number;
                               FJG = false;
                           }

                       }
                   }

                   //检测收货地址
                   strsql = "/*dialect*/ select distinct F_PAEZ_DZ,F_PAEZ_PO,F_PAEZ_LXFS  from KD_BCC_IMPORTEntry where FID =" + FID + " and F_PAEZ_LYBILLNO='" + F_PAEZ_LYBILLNO + "'";
                   DSC = DBUtils.ExecuteDataSet(this.Context, strsql);
                   DTC = DSC.Tables[0];
                   if (DTC.Rows.Count > 0)
                   {
                       for (t = 0; t < DTC.Rows.Count; t++)
                       {
                           F_PAEZ_DZ = DTC.Rows[t]["F_PAEZ_DZ"].ToString();
                           F_PAEZ_PO = DTC.Rows[t]["F_PAEZ_PO"].ToString();
                           F_PAEZ_LXFS = DTC.Rows[t]["F_PAEZ_LXFS"].ToString();

                           if ((F_PAEZ_DZ.Split(new char[] { ',' }).Length - 1) != 3)
                           {
                               FInspecData = FInspecData+"来源订单号:" + F_PAEZ_LYBILLNO + ",地址:" + F_PAEZ_DZ + "不规范无法识别！";
                               FJG = false;
                           }
                           else
                           {
                              
                                s = F_PAEZ_DZ.Split(',');
                                FREGION = s[0].ToString();
                                FCITY = s[1].ToString();
                                FCOUNTY = s[2].ToString();
                                FADDRESS = s[3].ToString();

                                FFULLADDRESS ="中国"+ " " + FREGION + " " + FCITY + " " + FCOUNTY + "  " + FADDRESS;


                               strsql = "/*dialect*/ select count(*) from T_ECC_LOGISTICSAREADETAIL_L where FDATAVALUE ='" + FREGION + "'";
                               FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                               if (FCount == "0")
                               {
                                   FInspecData = FInspecData + "来源订单号:" + F_PAEZ_LYBILLNO + ",地址:" + F_PAEZ_DZ + "省份不存在！";
                                   FJG = false;
                               }
                               else
                               {
                                   strsql = "/*dialect*/ select FDETAILID from T_ECC_LOGISTICSAREADETAIL_L where FDATAVALUE ='" + FREGION + "'";
                                   FREGIONID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                                   strsql = "/*dialect*/ select FNumber from T_ECC_LOGISTICSAREADETAIL where FDETAILID ='" + FREGIONID + "'";
                                   FREGIONNo = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                               }

                               strsql = "/*dialect*/ select count(*) from T_ECC_LOGISTICSAREADETAIL_L where FDATAVALUE ='" + FCITY + "'";
                               FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                               if (FCount == "0")
                               {
                                   FInspecData = FInspecData + "来源订单号:" + F_PAEZ_LYBILLNO + ",地址:" + F_PAEZ_DZ + "市不存在！";
                                   FJG = false;
                               }
                               else
                               {
                                   strsql = "/*dialect*/ select FDETAILID from T_ECC_LOGISTICSAREADETAIL_L where FDATAVALUE ='" + FCITY + "'";
                                   FCITYID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                                   strsql = "/*dialect*/ select FNumber from T_ECC_LOGISTICSAREADETAIL where FDETAILID ='" + FCITYID + "'";
                                   FCITYNo = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                               }

                               strsql = "/*dialect*/ select count(*) from T_ECC_LOGISTICSAREADETAIL_L where FDATAVALUE ='" + FCOUNTY + "'";
                               FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                               if (FCount == "0")
                               {
                                   FInspecData = FInspecData + "来源订单号:" + F_PAEZ_LYBILLNO + ",地址:" + F_PAEZ_DZ + "区/县不存在！";
                                   FJG = false;
                               }
                               else
                               {
                                   strsql = "/*dialect*/ select FDETAILID from T_ECC_LOGISTICSAREADETAIL_L where FDATAVALUE ='" + FCOUNTY + "'";
                                   FCOUNTYID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                                   strsql = "/*dialect*/ select FNumber from T_ECC_LOGISTICSAREADETAIL where FDETAILID ='" + FCOUNTYID + "'";
                                   FCOUNTYNo = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);
                               }


                           }
                           if (F_PAEZ_LXFS.IndexOf(",") == -1)
                           {
                               FPHONE = F_PAEZ_LXFS;
                           }
                           else
                           {

                               FPHONE = F_PAEZ_LXFS.Substring(F_PAEZ_LXFS.IndexOf(",") + 1, F_PAEZ_LXFS.Length - (F_PAEZ_LXFS.IndexOf(",") + 1));
                           }
                           //判断收获地址是否需要新增
                           if (FJG == true)
                           {
                               strsql = "/*dialect*/ select count(*) from T_ECC_ADDRESS where FREGIONID=" + FREGIONID + "and FCITYID=" + FCITYID + "and FCOUNTYID=" + FCOUNTYID + " and  " +
                                         "FRECEIVEPERSON ='" + F_PAEZ_PO + "' and FADDRESS =replace('" + FADDRESS + "',' ','') and FMOBILE ='" + FPHONE + "'";
                               FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                               if (FCount == "0")
                               {

                                   strsql = "/*dialect*/ select '" + F_PAEZ_PO + "' as FRECEIVEPERSON, '" + FREGIONNo + "' as FREGIONID, '" + FCITYNo + "' as FCITYID, '" + FCOUNTYNo + "' as FCOUNTYID, '" + FADDRESS + "' as FADDRESS, '" + FPHONE + "' as FMOBILE, '" + FFULLADDRESS + "' as FFULLADDRESS";
                                   DS = DBUtils.ExecuteDataSet(this.Context, strsql);

                                   DT = DS.Tables[0];


                                   IMPORTADDRESS im = new IMPORTADDRESS();
                                   im.SAL_B2CADDRESS(DS).ToString();

                                   strsql = "/*dialect*/ select  top 1 FID from T_ECC_ADDRESS where FREGIONID=" + FREGIONID + "and FCITYID=" + FCITYID + "and FCOUNTYID=" + FCOUNTYID + " and  " +
                                            "FRECEIVEPERSON ='" + F_PAEZ_PO + "'  and FFULLADDRESS ='" + FFULLADDRESS + "' and FMOBILE ='" + FPHONE + "'";
                                   FADDRESSID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                                   strsql = "/*dialect*/ update  KD_BCC_IMPORTEntry set FFULLADDRESS='" + FADDRESSID + "' where FID =" + FID + " and F_PAEZ_LYBILLNO='" + F_PAEZ_LYBILLNO + "'";
                                   DBUtils.Execute(this.Context, strsql);

                                   DS.Clear();
                                   DT.Clear();
                               }
                               else
                               {

                                   strsql = "/*dialect*/ select top 1 FID from T_ECC_ADDRESS where FREGIONID=" + FREGIONID + "and FCITYID=" + FCITYID + "and FCOUNTYID=" + FCOUNTYID + " and  " +
                                            "FRECEIVEPERSON ='" + F_PAEZ_PO + "'  and FFULLADDRESS ='" + FFULLADDRESS + "' and FMOBILE ='" + FPHONE + "'";
                                   FADDRESSID = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                                   strsql = "/*dialect*/ update  KD_BCC_IMPORTEntry set FFULLADDRESS='" + FADDRESSID + "' where FID =" + FID + " and F_PAEZ_LYBILLNO='" + F_PAEZ_LYBILLNO + "'";
                                   DBUtils.Execute(this.Context, strsql);
                               }
                           }
                       }
                   }

               }
           }

           return FJG;
       }


       /// <summary>
       /// 检测来源订单号是否已生成B2C订单
       /// </summary>
       /// <param name="FID"></param>
       /// <returns></returns>
       public bool FInspectBCC(string FID)
       {
           string strsql, FCount, F_PAEZ_LYBILLNO;
           bool FJG = true;
           FInspectext = "";
           int i;
           DataSet DSSK;
           DataTable DTSK;

           strsql = "/*dialect*/ select distinct F_PAEZ_LYBILLNO from KD_BCC_IMPORTEntry where FID =" + FID + " and  F_BCB_BILLNO=''";

           DSSK = DBUtils.ExecuteDataSet(this.Context, strsql);
           DTSK = DSSK.Tables[0];
           if (DTSK.Rows.Count > 0)
           {
               for (i = 0; i < DTSK.Rows.Count; i++)
               {
                   F_PAEZ_LYBILLNO = DTSK.Rows[i]["F_PAEZ_LYBILLNO"].ToString();

                   strsql = "/*dialect*/ select count(*) from T_ECC_NETORDER where FSOURCEORDERNUM='" + F_PAEZ_LYBILLNO + "'";
                   FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                   if (FCount != "0")
                   {
                       FJG = false;
                       FInspectext = FInspectext + "来源订单号" + F_PAEZ_LYBILLNO + "已生成过B2C订单，请检查数据！";
                   }

               }
           }

           return FJG;

       }

         public void FJCBCCNo(string FID)
         {


             string strsql, FCount, F_PAEZ_LYBILLNO, FBILLNO;
   
             int i;
             DataSet DSSK;
             DataTable DTSK;

             strsql = "/*dialect*/ select distinct F_PAEZ_LYBILLNO from KD_BCC_IMPORTEntry where FID =" + FID + "";

             DSSK = DBUtils.ExecuteDataSet(this.Context, strsql);
             DTSK = DSSK.Tables[0];
             if (DTSK.Rows.Count > 0)
             {
                 for (i = 0; i < DTSK.Rows.Count; i++)
                 {
                     F_PAEZ_LYBILLNO = DTSK.Rows[i]["F_PAEZ_LYBILLNO"].ToString();
                     

                     strsql = "/*dialect*/ select count(*) from T_ECC_NETORDER where FSOURCEORDERNUM='" + F_PAEZ_LYBILLNO + "'";
                     FCount = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                     if (FCount == "0")
                     {
                         strsql = "/*dialect*/ update KD_BCC_IMPORTEntry set F_BCB_BILLNO ='' where F_PAEZ_LYBILLNO = '" + F_PAEZ_LYBILLNO + "'";
                         DBUtils.Execute(this.Context, strsql);
                     }

                     else
                     {

                         strsql = "/*dialect*/ select FBILLNO from T_ECC_NETORDER where FSOURCEORDERNUM='" + F_PAEZ_LYBILLNO + "'";
                         FBILLNO = DBUtils.ExecuteScalar<string>(this.Context, strsql, string.Empty, null);

                         strsql = "/*dialect*/ update KD_BCC_IMPORTEntry set F_BCB_BILLNO ='" + FBILLNO + "' where F_PAEZ_LYBILLNO = '" + F_PAEZ_LYBILLNO + "'";
                         DBUtils.Execute(this.Context, strsql);
                     }

                 }
             }
         }



       public void FSETBCC(string FID)
       {
           int i;
           string strsql, F_PAEZ_LYBILLNO;
           

           DataSet DSBCC;
           DataTable DTBCC;
           DataSet DSBCCE;
           DataTable DTBCCE;


           DataSet DSA;
           DataTable DTA;

           strsql = "/*dialect*/ select distinct F_PAEZ_LYBILLNO from KD_BCC_IMPORTEntry where FID =" + FID + "";
           DSA = DBUtils.ExecuteDataSet(this.Context, strsql);
           DTA = DSA.Tables[0];
           if (DTA.Rows.Count > 0)
           {
               for (i = 0; i < DTA.Rows.Count; i++)
               { 
                   F_PAEZ_LYBILLNO = DTA.Rows[i]["F_PAEZ_LYBILLNO"].ToString();

                   strsql = "/*dialect*/ select top 1 assd.FID AS FAESID,  F_XS_DATES as FNETORDERDATE, F_PAEZ_LYBILLNO,shop.FNUMBER as FNETORDERSOURCE,F_PAEZ_PO as FReceivePerson,assd.FMOBILE as FMobilePhone,assd.FFULLADDRESS as FFullAddress from KD_BCC_IMPORTEntry t1 " +
                    " left join T_ECC_SHOP shop (nolock) on t1.FNETORDERSOURCE =shop.FID " +
                    " left join T_ECC_ADDRESS assd (nolock) on assd.FID =isnull(t1.FFULLADDRESS,0) " +
                    " where t1.FID ="+ FID +" and F_PAEZ_LYBILLNO ='" + F_PAEZ_LYBILLNO + "'";

                   DSBCC = DBUtils.ExecuteDataSet(this.Context, strsql);

                   DTBCC = DSBCC.Tables[0];

                   strsql = "/*dialect*/ select distinct t2.FNUMBER as FMaterialId,t4.FNUMBER as FSaleUnit,'' as FTJNumber ,t1.F_CG_QTY as FQty,F_CG_PRICE as FTaxPrice,t5.FNUMBER as FStockOrgId , t6.FNUMBER as FStockId,t1.F_SP_NUMBER ,t1.F_SP_NAME from KD_BCC_IMPORTEntry t1 " +
                            "inner join T_BD_MATERIAL t2 (nolock) on t1.F_PAEZ_WL=t2.FMATERIALID " +
                            "inner join t_BD_MaterialStock t3 (nolock)on t3.FMATERIALID =t2.FMATERIALID " +
                            "inner join T_BD_UNIT t4 (nolock)on t4.FUNITID  =t3.FSTOREUNITID " +
                            "inner join T_ORG_Organizations t5 (nolock)on t5.FORGID  =t1.F_PAEZ_ORGID " +
                            "inner join T_BD_STOCK t6 (nolock)on t6.FSTOCKID  =t1.F_PAEZ_STOCK " +
                            "where t1.FID =" + FID + " and F_PAEZ_LYBILLNO ='" + F_PAEZ_LYBILLNO + "' and  F_PAEZ_WL<> 0 " +
                            "union all " +
                            " select distinct   t2.FNUMBER as FMaterialId,t4.FNUMBER as FSaleUnit,t8.FNUMBER as FTJNumber  ,t5.FQTY*t1.F_CG_QTY as FQty,F_CG_PRICE/(select sum(FQTY)  from T_ECC_SuiteMaterials where FID =t5.FID) as FTaxPrice,t6.FNUMBER as FStockOrgId , t7.FNUMBER as FStockId,t1.F_SP_NUMBER ,t1.F_SP_NAME from KD_BCC_IMPORTEntry t1 " +
                            "inner join T_ECC_SuiteMaterials t5 on t1.F_PAEZ_TJ =t5.FID " +
                            "inner join T_ECC_Suite t8 on t8.FID =t5.FID " +
                            "inner join T_BD_MATERIAL t2 (nolock)on t5.FMATERIALID =t2.FMATERIALID " +
                            "inner join t_BD_MaterialStock t3 (nolock)on t3.FMATERIALID =t2.FMATERIALID " +
                            "inner join T_BD_UNIT t4 (nolock)on t4.FUNITID  =t3.FSTOREUNITID " +
                            "inner join T_ORG_Organizations t6 (nolock)on t6.FORGID  =t1.F_PAEZ_ORGID " +
                            "inner join T_BD_STOCK t7 (nolock)on t7.FSTOCKID  =t1.F_PAEZ_STOCK  " +
                            " where t1.FID =" + FID + " and F_PAEZ_LYBILLNO ='" + F_PAEZ_LYBILLNO + "' and  F_PAEZ_TJ<> 0";
                  DSBCCE = DBUtils.ExecuteDataSet (this.Context, strsql);
                  DTBCCE = DSBCCE.Tables[0];

                  DataSet ds = new DataSet();
                  DataTable dt1 = new DataTable();
                  DataTable dt2 = new DataTable();

                  dt1 = DTBCC.Copy();
                  dt2 = DTBCCE.Copy();

                  dt1.TableName = "B2C";
                  dt2.TableName = "B2CEntry";

                  ds.Tables.Add(dt1);
                  ds.Tables.Add(dt2);

                   IMPORTB2C im = new IMPORTB2C();
                   FBillNo= im.SAL_B2CSEODER(ds).ToString();

                   setBiLLno(FBillNo, FID, F_PAEZ_LYBILLNO);


                   DSBCC.Clear();
                   DSBCCE.Clear();
               }
           }

          
       }

     public void setBiLLno(string FBillNo,string FID , string F_PAEZ_LYBILLNO)
       {
           string strsql;

           if (FBillNo !="")
           { 
           strsql = "/*dialect*/ update KD_BCC_IMPORTEntry set F_BCB_BILLNO = '" + FBillNo + "' where FID =" + FID + " and F_PAEZ_LYBILLNO ='" + F_PAEZ_LYBILLNO + "'";
           DBUtils.Execute(this.Context, strsql);

           }
       }
    

    }
}
